function loginpage() {
    // layer.open({
    //     type: 1,
    //     content: $('#id') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
    // });
    loginAjax();
}

function loginAjax(email,password) {
    $.ajax({
        url: "/loginUser",
        type: 'POST',
        data: {
            "email": email,
            "password": password
        },
        success: function (result) {
            console.log(result);
            if (result.length) {
                window.sessionStorage.setItem('email', result[0].email);
                window.sessionStorage.setItem('token', result[0].token);
                window.sessionStorage.setItem('branch', result[0].branch);
                window.sessionStorage.setItem('owner', result[0].owner);
                window.sessionStorage.setItem('repo', result[0].repo);
                layer.alert('登陆成功', function () {
                    window.location.href = '/user';
                })
            }else{
                layer.msg('登陆失败');
            }
        }
    });
}


function regAjax(email,password) {
    $.ajax({
        url: "/addUserInfo",
        type: 'POST',
        data: {
            "email": email,
            "password": password
        },
        success: function (result) {
            console.log(result);
            if (result.insertId) {
                window.sessionStorage.setItem('email', result[0].email);
                window.sessionStorage.setItem('token', result[0].token);
                window.sessionStorage.setItem('branch', result[0].branch);
                window.sessionStorage.setItem('owner', result[0].owner);
                window.sessionStorage.setItem('repo', result[0].repo);
                layer.alert('注册成功', function () {
                    window.location.href = '/user';
                })
            }else{
                layer.msg('注册失败');
            }
        }
    });
}


function saveOrUpdateToekn() {
    $.ajax({
        url: "/updateUserInfo",
        type: 'POST',
        data: {
            "email": $("input[name=email]").val(),
            "token": $("input[name=token]").val(),
            "branch": $("input[name=branch]").val(),
            "owner": $("input[name=owner]").val(),
            "repo": $("input[name=repo]").val()
        },
        success: function (result) {
            console.log(result);
            if (result) {
                layer.alert('更新成功', function () {
                    window.sessionStorage.setItem('email', $("input[name=email]").val());
                    window.sessionStorage.setItem('token', $("input[name=token]").val());
                    window.sessionStorage.setItem('branch', $("input[name=branch]").val());
                    window.sessionStorage.setItem('owner', $("input[name=owner]").val());
                    window.sessionStorage.setItem('repo', $("input[name=repo]").val());
                    window.location.href = '/user';
                })
            }
        }
    });
}


function loginout() {
    window.sessionStorage.removeItem('email');
    window.sessionStorage.removeItem('token');
    window.sessionStorage.removeItem('branch');
    window.sessionStorage.removeItem('owner');
    window.sessionStorage.removeItem('repo');
    if (!window.sessionStorage.getItem('email')) {
        layer.alert('退出登录成功', function () {
            window.location.href = '/';
        })
    }
}


$(function () {
    if (window.sessionStorage.getItem('email')) {
        $($(".user-center")[1]).remove();
        $($(".user-center")[0]).show();
    } else {
        $($(".user-center")[0]).remove();
        $($(".user-center")[0]).show();
    }
})


function loginQhbtn(index) {
    if (index) {
        $(".reg-btn").show();
        $(".login-btn").hide();
    } else {
        $(".login-btn").show();
        $(".reg-btn").hide();
    }
}


function loginUser() {
    let email = $("input[name=email]").val();
    let password = $("input[name=password]").val();
    if(jy(0)){
        loginAjax(email,password);
    }
}

function regUser() {
    let email = $("input[name=email]").val();
    let password = $("input[name=password]").val();
    if(jy(1)){
        regAjax(email,password);
    }
}

function jy(index) {
    let email = $("input[name=email]").val();
    let password = $("input[name=password]").val();
    let passwordtwo = $("input[name=password-two]").val();
    let result = true;
    result = emailfun(email);
    if(result){
        result = passwordfun(password);
    }
    if (index) {
        result = passwordtwofun(password,passwordtwo);
        return result;
    }else{
        return result;
    }

}

function emailfun(email) {
    var emailreg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
    if (email === "") { //输入不能为空
        layer.alert("邮箱输入不能为空!");
        return false;
    } else if (!emailreg.test(email)) { //正则验证不通过，格式不对
        layer.alert("邮箱格式不正确!");
        return false;
    } else {
        return true;
    }
}

function passwordfun(password) {
    var pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}');
    if (!pwdRegex.test(password)) {
        layer.alert("您的密码复杂度太低（密码中必须包含字母、数字）！");
        return false;
    }else{
        return true;
    }
}

function passwordtwofun(password,passwordtwo){
    if(password == passwordtwo){
        return true;
    }else{
        layer.alert("两次密码不相同！");
        return false;
    }
}